package com.woniu.bh.beautysalon.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.beautysalon.model.dto.*;
import com.woniu.bh.beautysalon.model.params.ArrangeTechnicianParam;
import com.woniu.bh.beautysalon.web.annotation.CloudLock;
import com.woniu.bh.beautysalon.web.dao.*;
import com.woniu.bh.beautysalon.web.service.ITechnicianService;
import com.woniu.bh.common.core.domain.beautysalon.Beautysalon;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonCheck;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonSign;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonTechnicianMessage;
import com.woniu.bh.common.core.domain.order.Orders;
import com.woniu.bh.common.core.domain.technician.CheckPic;
import com.woniu.bh.common.core.domain.technician.Technician;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cxb
 * @since 2020-10-13
 */
@Service
public class TechnicianServiceImpl extends ServiceImpl<TechnicianMapper, Technician> implements ITechnicianService {
    @Autowired
    private TechnicianMapper technicianMapper;

    @Autowired
    private BeautysalonSignMapper beautysalonSignMapper;

    @Autowired
    private BeautysalonTechnicianMessageMapper btmm;

    @Autowired
    private BeautysalonMapper bm;

    @Autowired
    private BeautysalonCheckMapper bcm;
    @Autowired
    private CheckPicMapper cpm;
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * @description: 美容师排行榜
     * @param: beautysalonId
     * @return: java.util.List<com.woniu.bh.beautysalon.model.dto.TechnicianRankDto>
     */
    @Override
    public List<TechnicianRankDto> technicianRank(Integer beautysalonId) {
        return technicianMapper.technicianRank(beautysalonId);
    }

    /**
     * @description: 所有美容师分页查询
     * @param: pageIndex
     * @param: pageSize
     * @param: technicianUsername
     * @param: technicianSex
     * @param: beforeAge
     * @param: afterAge
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.AllTechniciansDto>
     */
    @Override
    public IPage<AllTechniciansDto> pageQuery(Integer pageIndex, Integer pageSize, String technicianName, String technicianSex, Integer beforeAge, Integer afterAge) {
        Page<AllTechniciansDto> page = new Page<>(pageIndex, pageSize);
        return technicianMapper.pageQuery(page, technicianName, technicianSex, beforeAge, afterAge);
    }

    /**
     * @description: 查看美容师详情
     * @param: technicianId
     * @return: com.woniu.bh.beautysalon.model.dto.TechnicianDetailDto
     */
    @Override
    public TechnicianDetailDto getOneTechnician(Integer technicianId) {
        Technician technician = technicianMapper.selectById(technicianId);
        TechnicianDetailDto technicianDetailDto = new TechnicianDetailDto();
        BeanUtils.copyProperties(technician, technicianDetailDto);
        //查询美容师提供的服务
        List<Object> masterService = technicianMapper.masterService(technicianId);
        List<Object> obj = new ArrayList<>(masterService);
        technicianDetailDto.setMasterService(obj);
        return technicianDetailDto;
    }

    /**
     * @description: 签约美容师
     * @param: beautysalonId
     * @param: technicianId
     * @return: int
     */
    @Override
    public int signTechnician(Integer beautysalonId, Integer technicianId) {
        Technician technician = technicianMapper.selectById(technicianId);
        Integer technicianBeautysalonId = technician.getTechnicianBeautysalonId();
        if (technicianBeautysalonId != null) {
            return -1;
        }
        //新增数据到数据库
        BeautysalonSign beautysalonSign = new BeautysalonSign();
        beautysalonSign.setSignBeautysalonId(beautysalonId);
        beautysalonSign.setSignStatus("wait");
        beautysalonSign.setSignTechnicianId(technicianId);
        beautysalonSign.setSignTime(System.currentTimeMillis());
        beautysalonSignMapper.insert(beautysalonSign);
        Integer signId = beautysalonSign.getSignId();
        //发送消息
        BeautysalonTechnicianMessage btm = new BeautysalonTechnicianMessage();
        //接收者id
        btm.setBeautysalonTechnicianMessageAcceptTechnicianId(technicianId);
        Beautysalon beautysalon = bm.selectById(beautysalonId);
        String beautysalonName = beautysalon.getBeautysalonName();
        //发送内容
        btm.setBeautysalonTechnicianMessageContent("美容馆" + "<" + beautysalonName + ">" + "请求与您签约,快快去处理哦...");
        //发送者id
        btm.setBeautysalonTechnicianMessageSendBeautysalonId(beautysalonId);
        //设置时间
        btm.setBeautysalonTechnicianMessageTime(System.currentTimeMillis());
        btmm.insert(btm);
        return signId;
    }

    /**
     * @description: 美容馆签约美容师查询
     * @param: pageIndex
     * @param: pageSize
     * @param: beautysalonId
     * @param: technicianUserName
     * @param: technicianSex
     * @param: beforeAge
     * @param: afterAge
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.AllTechniciansDto>
     */
    @Override
    public IPage<AllTechniciansDto> getAllSignedTechnicians(Integer pageIndex, Integer pageSize, Integer beautysalonId, String technicianName, String technicianSex, Integer beforeAge, Integer afterAge) {
        Page<AllTechniciansDto> page = new Page<>(pageIndex, pageSize);
        return technicianMapper.getMyTechnicians(page, beautysalonId, technicianName, technicianSex, beforeAge, afterAge);
    }

    /**
     * @description: 解约
     * @param: technicianId
     * @return: int
     */
    @Override
    public int termination(Integer technicianId) {
        Technician technician1 = technicianMapper.selectById(technicianId);
        if (technician1.getTechnicianWorkstatus()==1){
            return -1;
        }
        Technician technician = new Technician();
        technician.setTechnicianBeautysalonId(-1);
        technician.setTechnicianId(technicianId);
        return technicianMapper.updateById(technician);
    }

    /**
     * @description: 查看提交审核的美容师
     * @param: pageIndex
     * @param: pageSize
     * @param: beautysalonId
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.CheckDto>
     */
    @Override
    public IPage<CheckDto> getAllCheck(Integer pageIndex, Integer pageSize, Integer beautysalonId) {
        Page<CheckDto> page = new Page<>(pageIndex, pageSize);
        return technicianMapper.getAllCheck(page, beautysalonId);
    }

    /**
     * @description: 安排美容师
     * @param: arrangeTechnicianParam
     * @return: void
     */
    @Override

    public void arrangeTechnician(ArrangeTechnicianParam arrangeTechnicianParam) {
        Orders orders = new Orders();
        orders.setTechnicianId(arrangeTechnicianParam.getTechnicianId());
        orders.setOrdersId(arrangeTechnicianParam.getOrderId());
        ordersMapper.updateById(orders);

        Technician technician = new Technician();
        technician.setTechnicianWorkstatus(1);
        technician.setTechnicianId(arrangeTechnicianParam.getTechnicianId());
        technicianMapper.updateById(technician);

    }

    /**
     * @description: 查看某个审核的美容师详情
     * @param: checkId
     * @return: com.woniu.bh.beautysalon.model.dto.TechnicianCheckDetailDto
     */
    @Override
    public TechnicianCheckDetailDto getOneCheck(Integer checkId) {
        BeautysalonCheck beautysalonCheck = bcm.selectById(checkId);
        TechnicianCheckDetailDto tcdd = new TechnicianCheckDetailDto();
        BeanUtils.copyProperties(beautysalonCheck, tcdd);
        QueryWrapper<CheckPic> cpqw = new QueryWrapper<>();
        cpqw.eq("check_pic_check_id", checkId);
        List<CheckPic> checkPics = cpm.selectList(cpqw);
        List<CheckPic> checkPicAddress = new ArrayList<>(checkPics);
        tcdd.setCheckPicAddress(checkPicAddress);
        //查询美容师提供的服务
        Integer technicianId = beautysalonCheck.getCheckTechnicianId();
        List<Object> masterService = technicianMapper.masterService(technicianId);
        List<Object> obj = new ArrayList<>(masterService);
        tcdd.setMasterService(obj);
        return tcdd;
    }

    /**
     * @description: 审核通过，发送消息
     * @param: checkId
     * @param: message
     * @param: beautysalonId
     * @return: int
     */
    @Override
    public int sendOkMsg(Integer checkId, String message, Integer beautysalonId) {
        BeautysalonCheck beautysalonCheck = bcm.selectById(checkId);
        Integer checkTechnicianId = beautysalonCheck.getCheckTechnicianId();
        //绑定场馆id
        Technician technician = new Technician();
        technician.setTechnicianId(checkTechnicianId);
        technician.setTechnicianBeautysalonId(beautysalonId);
        technicianMapper.updateById(technician);
        //发送消息
        BeautysalonTechnicianMessage btm = new BeautysalonTechnicianMessage();
        btm.setBeautysalonTechnicianMessageSendBeautysalonId(beautysalonId);
        btm.setBeautysalonTechnicianMessageContent(message);
        btm.setBeautysalonTechnicianMessageAcceptTechnicianId(checkTechnicianId);
        btmm.insert(btm);
        return btm.getBeautysalonTechnicianMessageId();
    }

    /**
     * @description: 审核未通过，发送消息
     * @param: checkId
     * @param: message
     * @param: beautysalonId
     * @return: int
     */
    @Override
    public int sendNoMsg(Integer checkId, String message, Integer beautysalonId) {
        BeautysalonCheck beautysalonCheck = bcm.selectById(checkId);
        Integer checkTechnicianId = beautysalonCheck.getCheckTechnicianId();
        //发送消息
        BeautysalonTechnicianMessage btm = new BeautysalonTechnicianMessage();
        btm.setBeautysalonTechnicianMessageSendBeautysalonId(beautysalonId);
        btm.setBeautysalonTechnicianMessageContent(message);
        btm.setBeautysalonTechnicianMessageAcceptTechnicianId(checkTechnicianId);
        btm.setBeautysalonTechnicianMessageTime(System.currentTimeMillis());
        btmm.insert(btm);
        return btm.getBeautysalonTechnicianMessageId();
    }
}
